<?php
// $Id:
global $config;
require_once(APP_LIB_DIR . 'UserRightModel.class.php');
require_once(APP_LIB_DIR . 'LookupModel.class.php');
include_once(APP_LIB_DIR . 'JSON.php');
require_once(APP_LIB_DIR . '/LoginModel.class.php');
        Class LoginAction extends Action
        {
            function getDefaultView(&$controller, &$request, &$user)
            {
                #if($user->isAuthenticated()){
                #    $controller->forward(DEFAULT_MODULE, 'AdminConsole');
                #    return VIEW_NONE;
                #}
                return VIEW_INPUT;
            }
            
            function getRequestMethods()
            {
                return REQ_POST | REQ_GET;
            }
            
            function getparameters($request)
            {
                $param = Array();
                $param['username'] = $request->getParameter('username');
                $param['password'] = $request->getParameter('pass');
                $param['flag'] = $request->getParameter('flag');
                return $param;
            }
            
            function execute(&$controller, &$request, &$user)
            {
                $lm = new LoginModel('LoginAction');
                $param = $this->getparameters($request);
				$ip_address = $_SERVER['REMOTE_ADDR'];
				$ur = new UserRightModel('LoginAction');
                
                switch($param['flag']){
                    case "Login":
                        if(!$param['username'] || !$param['password']){
                            $request->setAttribute('trigger', 0);
                            return VIEW_INPUT;
                        }
                        $authentication = $lm->authenticate_username($param['username'], $param['password'],$ip_address);
                        if($authentication){
                            # Set authentication parameters.
                            $user->setAuthenticated(true);         
                            $user->setAttribute('auth', $authentication);
							$user->addPrivilege(strtolower($authentication['privilege']));

							# Set Privilege per Page
							foreach($ur->get_priv($authentication['privilege']) as $data){
     						  $user->addPrivilege($data['priv']);
    						}
                        
                            # Redirect to default admin page.
                            $controller->forward(DEFAULT_MODULE, 'DefaultIndex');
                    return VIEW_NONE;
                }else{
                    $user->setAuthenticated(false);
                    $request->setAttribute('alert', 'Invalid Username, Password or Access Level. Please try again.');
                    $request->setAttribute('trigger', 1);
                }           
            break;
        }
        return VIEW_INPUT;
    }
	function lookup($what)
	{
          $lm     = new LookupModel('LoginAction');
          $json   = new Services_JSON();
          
          switch($what){
            case 'privilege_level':
	      return $json->encode($lm->privilege_level());break;
            default:
	      return $json->encode($lm->privilege());break;
          }
	}
}
?>
